<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
          <el-form-item prop="search" :label="$t('common.search')">
            <el-input v-model="state.queryForm.search"
              :placeholder="$t('OrderRefund.input') + $t('OrderRefund.orderId') + '/' + $t('OrderRefund.flowTradeNo') + '/' + $t('OrderRefund.outRefundNo')"
              clearable @clear="getDataList" style="width: 350px" />
          </el-form-item>


          <el-form-item :label="$t('OrderRefund.shopId')" prop="shopId">
            <ShopSelect @clear="getDataList" v-model="state.queryForm.shopId"></ShopSelect>
          </el-form-item>
          <!-- <el-form-item prop="orderId" :label="$t('OrderRefund.orderId')">
            <el-input v-model="state.queryForm.orderId" :placeholder="$t('OrderRefund.inputOrderIdTip')" clearable
              @clear="getDataList" />
          </el-form-item> -->

          <!-- <el-form-item prop="flowTradeNo" :label="$t('OrderRefund.flowTradeNo')">
            <el-input v-model="state.queryForm.flowTradeNo" :placeholder="$t('OrderRefund.inputFlowTradeNoTip')" clearable
              @clear="getDataList" />
          </el-form-item> -->
          <!-- <el-form-item prop="outRefundNo" :label="$t('OrderRefund.outRefundNo')">
            <el-input v-model="state.queryForm.outRefundNo" :placeholder="$t('OrderRefund.inputOutRefundNoTip')" clearable
              @clear="getDataList" />
          </el-form-item> -->

          <el-form-item :label="$t('OrderRefund.payMethod')" prop="payMethod">
            <el-select v-model="state.queryForm.payMethod" :placeholder="$t('OrderRefund.inputPayMethodTip')" clearable
              filterable @clear="getDataList">
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_order_pay_method"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('OrderRefund.state')" prop="state">
            <el-select v-model="state.queryForm.state" :placeholder="$t('OrderRefund.selectStateTip')" clearable
              filterable @clear="getDataList">
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_order_refund_state"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('OrderRefund.processState')" prop="processState">
            <el-select v-model="state.queryForm.processState" :placeholder="$t('OrderRefund.selectProcessStateTip')"
              clearable filterable @clear="getDataList">
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_order_refund_process_state"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('OrderRefund.applyType')" prop="applyType">
            <el-select v-model="state.queryForm.applyType" :placeholder="$t('OrderRefund.selectApplyTypeTip')" clearable
              filterable @clear="getDataList">
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_order_refund_apply_type"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('common.createTime')" prop="isHot">
            <el-date-picker v-model="state.queryForm.rangeTime" @change="getDataList" type="daterange" range-separator="~"
              :value-format="$dateTimePattern" :shortcuts="rangeTimeShortcuts"
              :start-placeholder="$t('common.startTime')" :end-placeholder="$t('common.endTime')">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button icon="search" type="primary" @click="getDataList">
              {{ $t('common.queryBtn') }}
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'bizs_OrderRefund_add'">
            {{ $t('common.addBtn') }}
          </el-button>
          <el-button plain :disabled="multiple" icon="Delete" type="primary" v-auth="'bizs_OrderRefund_del'"
            @click="handleDelete(selectObjs)">
            {{ $t('common.delBtn') }}
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'bizs_OrderRefund_export'" @exportExcel="exportExcel"
            class="ml10 mr20" style="float: right;" @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle" @selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" />
        <el-table-column prop="orderId" :label="$t('OrderRefund.orderId')" min-width="200" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row.orderId }}
            <SvgIcon name="ele-CopyDocument" class="icon_copy" @click="copyText(scope.row.orderId)" />
          </template>

        </el-table-column>

        <!--<el-table-column type="index" label="#" width="40" />-->
        <el-table-column prop="uid" :label="$t('OrderRefund.uid')" show-overflow-tooltip />
        <el-table-column prop="shopId" :label="$t('OrderRefund.shopId')" show-overflow-tooltip />
        <el-table-column prop="orderItemId" :label="$t('OrderRefund.orderItemId')" show-overflow-tooltip />
        <el-table-column prop="orderAmount" :label="$t('OrderRefund.orderAmount')" sortable="custom"
          show-overflow-tooltip />
        <el-table-column prop="payMethod" :label="$t('OrderRefund.payMethod')" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="biz_order_pay_method" :value="scope.row.payMethod"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column prop="flowTradeNo" :label="$t('OrderRefund.flowTradeNo')" show-overflow-tooltip />
        <el-table-column prop="outRefundNo" :label="$t('OrderRefund.outRefundNo')" show-overflow-tooltip />
        <el-table-column prop="state" :label="$t('OrderRefund.state')" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="biz_order_refund_state" :value="scope.row.state"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column prop="processState" :label="$t('OrderRefund.processState')" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="biz_order_refund_process_state" :value="scope.row.processState"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column prop="num" :label="$t('OrderRefund.num')" sortable="custom" show-overflow-tooltip />
        <el-table-column prop="amount" :label="$t('OrderRefund.amount')" sortable="custom" show-overflow-tooltip >
          <template #default="{row}">
              {{ $amountFormat(row.amount) }}
          </template>
        </el-table-column>
        <el-table-column prop="applyType" :label="$t('OrderRefund.applyType')" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="biz_order_refund_apply_type" :value="scope.row.applyType"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pics" :label="$t('OrderRefund.pics')" show-overflow-tooltip />
        <el-table-column prop="reason" :label="$t('OrderRefund.reason')" show-overflow-tooltip />
        <el-table-column prop="rejectReason" :label="$t('OrderRefund.rejectReason')" show-overflow-tooltip />

        <el-table-column prop="approvedTime" :label="$t('Order.time')" show-overflow-tooltip min-width="200" >
          <template #default="{row}">
            <div v-if="row.createTime">
                {{ $t('OrderRefund.createTime') }}: {{ row.createTime }}
            </div>
            <div v-if="row.approvedTime">
                {{ $t('OrderRefund.approvedTime') }}: {{ row.approvedTime }}
            </div>
            <div v-if="row.rejectTime">
                {{ $t('OrderRefund.rejectTime') }}: {{ row.rejectTime }}
            </div>
            <div v-if="row.refundTime">
                {{ $t('OrderRefund.refundTime') }}: {{ row.refundTime  }}
            </div>
          </template>
        </el-table-column>

        <!-- <el-table-column prop="createTime" :label="$t('OrderRefund.createTime')" show-overflow-tooltip />
        <el-table-column prop="approvedTime" :label="$t('OrderRefund.approvedTime')" show-overflow-tooltip />
        <el-table-column prop="rejectTime" :label="$t('OrderRefund.rejectTime')" show-overflow-tooltip />
        <el-table-column prop="refundTime" :label="$t('OrderRefund.refundTime')" show-overflow-tooltip /> -->
        <el-table-column :label="$t('common.action')" width="150">
          <template #default="scope">

            <el-button icon="CircleClose" text type="primary"  v-if="scope.row.processState == 0"
              @click="examineFn(scope.row.id)">{{ $t('OrderRefund.examine') }}</el-button>

            <el-button icon="View" text type="primary" v-auth="'bizs_OrderRefund_edit'"
              @click="formDialogRef.openDialog(scope.row.id)">{{ $t('OrderRefund.view') }}</el-button>
            <el-button icon="delete" text type="primary" v-auth="'bizs_OrderRefund_del'"
              @click="handleDelete([scope.row.id])">{{ $t('common.delBtn') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
    <Examine @getList="getDataList" ref="ExamineREF"></Examine>

  </div>
</template>

<script setup lang="ts" name="systemOrderRefund">
import Examine from "./examine.vue"
import { BasicTableProps, useTable } from "/@/hooks/table";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { getPage, delObjs  } from "/@/api/bizs/OrderRefund";
import commonFunction from "/@/utils/commonFunction";
const { copyText } = commonFunction();

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const ShopSelect = defineAsyncComponent(() => import("/@/components/ShopSelect/index.vue"))

// 定义查询字典
import { useDict } from '/@/hooks/dict';
import {rangeTimeShortcuts} from "/@/utils/date";

const { biz_order_refund_state, biz_order_refund_process_state, biz_order_refund_apply_type, biz_order_pay_method } = useDict('biz_order_refund_state', 'biz_order_refund_process_state', 'biz_order_refund_apply_type', 'biz_order_pay_method')
// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {
  },
  pageList: getPage
})
const ExamineREF = ref()

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/bizs/orderRefund/export', Object.assign(state.queryForm, { ids: selectObjs }), 'OrderRefund.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({ id }) => id);
  multiple.value = !objs.length;
};

const examineFn =(id:string)=>{
  ExamineREF.value.openDialog(id);
}


// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm(t('common.delConfirmText'));
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success(t('common.delSuccessText'));
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>
